package com.masterclient.app.modules.shop;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselibrary.bean.shop.DealerShippingDetailBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.modules.shop.adapter.DealerShippingDetailAdapter;
import com.masterclient.app.modules.shop.contract.DealerShippingDetailContract;
import com.masterclient.app.modules.shop.contract.DealerShippingDetailPresenter;
import com.masterclient.app.widget.TitlebarView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auther hjian
 * @date 2020/9/12.
 */
@Route(path = AroutePath.DEALER_SHIPPING_DETAIL_ACTIVITY)
public class DealerShippingDetailActivity extends BaseActivity<DealerShippingDetailPresenter> implements DealerShippingDetailContract.View {


    @BindView(R.id.dealer_shipping_detail_titlebar)
    TitlebarView dealerShippingDetailTitlebar;
    @BindView(R.id.dealer_shipping_detail_name)
    TextView dealerShippingDetailName;
    @BindView(R.id.dealer_shipping_detail_recycler)
    RecyclerView dealerShippingDetailRecycler;
    @BindView(R.id.dealer_shipping_detail_delivery)
    ImageView dealerShippingDetailDelivery;
    @BindView(R.id.dealer_shipping_detail_nodelivery)
    ImageView dealerShippingDetailNodelivery;
    @BindView(R.id.dealer_shipping_detail_first_num)
    TextView dealerShippingDetailFirstNum;
    @BindView(R.id.dealer_shipping_detail_first_unit)
    TextView dealerShippingDetailFirstUnit;
    @BindView(R.id.dealer_shipping_detail_first_price)
    TextView dealerShippingDetailFirstPrice;
    @BindView(R.id.dealer_shipping_detail_second_num)
    TextView dealerShippingDetailSecondNum;
    @BindView(R.id.dealer_shipping_detail_second_unit)
    TextView dealerShippingDetailSecondUnit;
    @BindView(R.id.dealer_shipping_detail_second_price)
    TextView dealerShippingDetailSecondPrice;
    @BindView(R.id.dealer_shipping_detail_free_layout)
    LinearLayout dealerShippingDetailFreeLayout;

    @Autowired(name = "id")
    String id;

    private DealerShippingDetailAdapter shippingDetailAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dealer_shipping_detail);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);

        dealerShippingDetailTitlebar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

        dealerShippingDetailRecycler.setLayoutManager(new LinearLayoutManager(this){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        dealerShippingDetailRecycler.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.top = ScreenUtils.dp2px(10);
            }
        });
        shippingDetailAdapter = new DealerShippingDetailAdapter();
        dealerShippingDetailRecycler.setAdapter(shippingDetailAdapter);
        shippingDetailAdapter.bindToRecyclerView(dealerShippingDetailRecycler);


        mPresenter.getShippingDetail(id);
    }

    @Override
    public void showData(DealerShippingDetailBean dataBean) {
        dealerShippingDetailName.setText(dataBean.getTitle());

        if (dataBean.getArea_group_infos() != null && dataBean.getArea_group_infos().size()>0) {
            shippingDetailAdapter.setDelivery_type(dataBean.getDelivery_type());
            shippingDetailAdapter.setNewData(dataBean.getArea_group_infos());
        }

        //是否送达
        if ("1".equals(dataBean.getOpen_default())){
            dealerShippingDetailDelivery.setImageResource(R.mipmap.dealer_radio_selected);
            dealerShippingDetailNodelivery.setImageResource(R.mipmap.dealer_radio_unselecte);
            dealerShippingDetailFreeLayout.setVisibility(View.VISIBLE);

            dealerShippingDetailFirstNum.setText(dataBean.getFirst_weight());
            dealerShippingDetailSecondNum.setText(dataBean.getSecond_weight());
            dealerShippingDetailFirstPrice.setText(dataBean.getFirst_price());
            dealerShippingDetailSecondPrice.setText(dataBean.getSecond_price());
            dealerShippingDetailFirstUnit.setText("0".equals(dataBean.getDelivery_type())?"kg":"件");
            dealerShippingDetailSecondUnit.setText("0".equals(dataBean.getDelivery_type())?"kg":"件");
        }else {
            dealerShippingDetailDelivery.setImageResource(R.mipmap.dealer_radio_unselecte);
            dealerShippingDetailNodelivery.setImageResource(R.mipmap.dealer_radio_selected);
            dealerShippingDetailFreeLayout.setVisibility(View.GONE);
        }

    }

    @Override
    public void showFailure(String msg) {
        ToastUtil.showToast(msg);
    }
}
